package com.framework.loippi.controller.subscribe;

import com.framework.loippi.controller.GenericController;
import com.framework.loippi.entity.Department;
import com.framework.loippi.entity.Patient;
import com.framework.loippi.entity.Principal;
import com.framework.loippi.entity.Subscribe;
import com.framework.loippi.service.DepartmentService;
import com.framework.loippi.service.DoctorService;
import com.framework.loippi.service.PatientService;
import com.framework.loippi.service.SubscribeService;
import com.framework.loippi.support.Message;
import com.framework.loippi.support.Pageable;
import com.framework.loippi.utils.ParameterUtils;
import com.framework.loippi.utils.StringUtil;
import com.qiniu.util.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Controller - 预约表
 *
 * @author wmj
 * @version 2.0
 */
@Controller("adminSubscribeController")
@RequestMapping({"/admin/subscribe"})
public class SubscribeController extends GenericController {

    @Resource
    private SubscribeService subscribeService;
    @Resource
    private DepartmentService departmentService;
    @Resource
    private DoctorService doctorService;
    @Resource
    private PatientService patientService;

    /**
     * 跳转添加页面
     *
     * @param model
     * @return
     */
    @RequestMapping(value = {"/add"}, method = {RequestMethod.GET})
    public String add(ModelMap model) {
        List<Department> departmentList = departmentService.findAll();
        List<Patient> patientList = patientService.findAll();
        model.addAttribute("deps", departmentList);
        model.addAttribute("pats", patientList);
        return "/admin/subscribe/add";
    }

    /**
     * 保存
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public String save(Subscribe subscribe, RedirectAttributes redirectAttributes) {
        subscribeService.save(subscribe);
        addFlashMessage(redirectAttributes, SUCCESS_MESSAGE);
        return "redirect:list.jhtml";
    }

    /**
     * 编辑
     */
    @RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
    public String edit(@PathVariable Long id, ModelMap model) {
        Subscribe subscribe = subscribeService.find(id);
        model.addAttribute("subscribe", subscribe);
        return "/admin/subscribe/edit";
    }

    /**
     * 确认预约
     */
    @RequestMapping(value = "/confirm/{id}", method = RequestMethod.GET)
    public @ResponseBody
    Message confirm(@PathVariable Long id) {
        Subscribe subscribe = subscribeService.find(id);
        subscribe.setStatus(2); //0取消，1待就诊，2进行中，3.已完成
        subscribeService.update(subscribe);

        return SUCCESS_MESSAGE;
    }

    /**
     * 预约完成
     */
    @RequestMapping(value = "/finish/{id}", method = RequestMethod.GET)
    public @ResponseBody
    Message finish(@PathVariable Long id) {
        Subscribe subscribe = subscribeService.find(id);
        subscribe.setStatus(3); //0取消，1待就诊，2进行中，3.已完成
        subscribeService.update(subscribe);

        return SUCCESS_MESSAGE;
    }

    /**
     * 详情
     */
    @RequestMapping(value = "/view/{id}", method = RequestMethod.GET)
    public String view(@PathVariable Long id, ModelMap model) {
        Subscribe subscribe = subscribeService.find(id);
        model.addAttribute("subscribe", subscribe);
        return "/admin/subscribe/view";
    }

    /**
     * 更新
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public String update(Subscribe subscribe, RedirectAttributes redirectAttributes) {
        subscribeService.update(subscribe);
        addFlashMessage(redirectAttributes, SUCCESS_MESSAGE);
        return "redirect:list.jhtml";
    }

    /**
     * 列表查询
     *
     * @param pageable
     * @param model
     * @return
     */
    @RequestMapping(value = {"/list"}, method = {RequestMethod.GET})
    public String list(Pageable pageable, HttpServletRequest request, ModelMap model) {
        Map<String, Object> paramter = ParameterUtils.getParametersMapStartingWith(request, "filter_");
        Map map = new HashMap();
        for (String key : paramter.keySet()) {
            if (!StringUtil.isEmpty(paramter.get(key).toString())) {
                map.put(key, paramter.get(key));
            }
        }
        Subject subject = SecurityUtils.getSubject();
        if (subject != null) {
            Principal principal = (Principal) subject.getPrincipal();
            if (principal != null && principal.getRolename().equals("doctor")) {
                map.put("doctorPhone", principal.getUsername());
            }
        }
        String keyword = request.getParameter("keyword");
        if (!StringUtils.isNullOrEmpty(keyword)) {
            map.put("docNameLike", keyword);
            map.put("depNameLike", keyword);
            map.put("paNameLike", keyword);
        }
        pageable.setParameter(map);
        model.addAttribute("paramter", paramter);
        model.addAttribute("page", this.subscribeService.findByPage(pageable));
        return "/admin/subscribe/list";
    }

    /**
     * 删除操作
     *
     * @param ids
     * @return
     */
    @RequestMapping(value = {"/delete"}, method = {RequestMethod.POST})
    public @ResponseBody
    Message delete(Long[] ids) {
        this.subscribeService.deleteAll(ids);
        return SUCCESS_MESSAGE;
    }
}
